﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Himp.Erp
{
    /// <summary>
    /// 物料信息
    /// </summary>
    public class MatlInfoService : CrudAppService<MatlInfo, MatlInfoDto, Guid, MatlInfoInput, MatlInfoCreateDto, MatlInfoUpdateDto>, IMatlInfoService
    {
        private IRepository<MatlInfo, Guid> _materialInfoRepository;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="projectConfigRepository"></param>
        public MatlInfoService(IRepository<MatlInfo, Guid> materialInfoRepository) : base(materialInfoRepository)
        {
            _materialInfoRepository = materialInfoRepository;
        }

        /// <summary>
        /// 获取物料信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public override async Task<PagedResultDto<MatlInfoDto>> GetListAsync(MatlInfoInput input)
        {
            var iQuery = await _materialInfoRepository.GetQueryableAsync();
            iQuery = iQuery.WhereIf(!string.IsNullOrEmpty(input.MatlCatgeCodg), i => i.MatlCatgeCodg == input.MatlCatgeCodg);

            if (input.Pi == 0)
            {
                var itemsDto = ObjectMapper.Map<List<MatlInfo>, List<MatlInfoDto>>(iQuery.ToList());

                return new PagedResultDto<MatlInfoDto>(itemsDto.Count, itemsDto);
            }
            else
            {
                var totalCount = iQuery.Count();
                var items = iQuery.Page(input.Pi, input.Ps);
                var itemsDto = ObjectMapper.Map<List<MatlInfo>, List<MatlInfoDto>>(items.ToList());

                return new PagedResultDto<MatlInfoDto>(totalCount, itemsDto);
            }
        }
    }
}
